package com.shopping.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.shopping.bean.Admin;
import com.shopping.bean.OrderGoods;
import com.shopping.bean.Page;
import com.shopping.bean.Review;
import com.shopping.bean.User;
import com.shopping.dao.AdminDao;
import com.shopping.dao.impl.AdminDaoImpl;
import com.shopping.service.AdminService;

public class AdminServiceImpl implements AdminService {

	AdminDao ad = new AdminDaoImpl();
	@Override
	public Map<String, Object> loginAdmin(String adminName, String password) {
		Admin admin = ad.selectAdmin(adminName, password);
		Map<String,Object> map = new HashMap();
		if(admin != null) {
			map.put("adminLogin",admin);
			System.out.println(map.size());
		}else {
			admin = ad.selectAdminByName(adminName);
			if(admin != null) {
				map.put("erroCode", "密码错误");//密码错误
			}else {
				map.put("erroCode", "管理员不存在");//用户名不存在
			}
		}
		return map;
	}


	@Override
	public Page<User> findAllUser(Map<String,String[]> condition) {
		List<User> list = ad.selectAllUser(condition);
		
		int pageSum = ad.selectUserTotalRows(condition);
		int pageNum = 1;
		int pageSize = 5;
		
		try {
			pageNum = Integer.parseInt(condition.get("pageNum")[0]);
		}catch(Exception o) {
			o.printStackTrace();
		}
		try {
			pageSize = Integer.parseInt(condition.get("pageSize")[0]);
		}catch(Exception o) {
			o.printStackTrace();
		}
		Page<User> user =new Page<User>(list, pageNum, pageSize, pageSum);
		return user;
	}

	@Override
	public int changeUser(User newUser, User oldUser) {
		
		return ad.updateUser(newUser, oldUser);
	}

	@Override
	public int delUserById(int userId) {
		
		return ad.deleteUserById(userId);
	}

	@Override
	public int changeOrderGoods(OrderGoods newOrderGoods, OrderGoods oldOrderGoods) {
		// TODO Auto-generated method stub
		return ad.updateOrderGoods(newOrderGoods, oldOrderGoods);
	}

	@Override
	public int delOrderGoodsById(int oId) {
		// TODO Auto-generated method stub
		return ad.deleteOrderGoodsById(oId);
	}

	@Override
	public int changeReview(Review newReview, Review oldReview) {
		// TODO Auto-generated method stub
		return ad.updateReview(newReview, oldReview);
	}

	@Override
	public int delReviewById(int rId) {
		// TODO Auto-generated method stub
		return ad.deleteReviewById(rId);
	}


	@Override
	public List<Review> findAllReview() {
		return null;
	}

}
